package com.superteam.zzl.model.Search;

import android.util.Log;
import android.view.View;

import com.superteam.zzl.activity.SearchResultActivity;
import com.superteam.zzl.app.MyApplication;
import com.superteam.zzl.cans.ContentURLs;
import com.superteam.zzl.retrofit.Search.SearchRetrofit;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by zhang on 2017/1/13.
 */

public class LoadData {

    private ResultData data;

    public LoadData(ResultData data) {
        this.data = data;
        loadData();
    }

    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://www.qiugonglue.com/").
                //addConverterFactory(new StringConverterFactory()).
                        addConverterFactory(ScalarsConverterFactory.create()).
                        addConverterFactory(GsonConverterFactory.create()).
                        build();
        SearchRetrofit sr = retrofit.create(SearchRetrofit.class);
        String s = MyApplication.search;
        //Log.e("data",s);
        Call<SearchData> call = sr.getData(ContentURLs.result_before+s+ContentURLs.result_after);
        call.enqueue(new Callback<SearchData>() {
            @Override
            public void onResponse(Call<SearchData> call, Response<SearchData> response) {
                if(response.body().getPlaceCount()>0) {
                    data.sendSearch((ArrayList<SearchData.PlaceListBean>) response.body().getPlaceList());
                }else{
                    SearchResultActivity.rl_1.setVisibility(View.GONE);
                }
                if(response.body().getTrendCount()>0) {
                    data.sendDyn((ArrayList<SearchData.TrendListBean>) response.body().getTrendList());
                }else{
                    SearchResultActivity.rl_2.setVisibility(View.GONE);
                }
                if(response.body().getGroupCount()>0){
                    data.sendGrop((ArrayList<SearchData.GroupListBean>) response.body().getGroupList());

                }else{
                    SearchResultActivity.rl_3.setVisibility(View.GONE);
                }
                if(response.body().getProductCount()>0) {
                    data.sendShop((ArrayList<SearchData.ProductListBean>) response.body().getProductList());
                }else{
                    SearchResultActivity.rl_4.setVisibility(View.GONE);
                }
                if(response.body().getUserCount()>0) {
                    data.sendUser((ArrayList<SearchData.UserListBean>) response.body().getUserList());
                }else{
                    SearchResultActivity.rl_5.setVisibility(View.GONE);
                }
                if(response.body().getPoiCount()>0) {
                    data.sendTra((ArrayList<SearchData.PoiListBean>) response.body().getPoiList());
                }else{
                    SearchResultActivity.rl_6.setVisibility(View.GONE);
                }
                Log.e("data","成功");
            }

            @Override
            public void onFailure(Call<SearchData> call, Throwable t) {
            }
        });
    }
}
